package com.ckm.controller;

import com.ckm.annotation.FindRedis;
import com.ckm.annotation.LogInfo;
import com.ckm.dto.StudentCourseInfoDto;
import com.ckm.entity.ResponseResult;
import com.ckm.entity.Student;
import com.ckm.entity.StudentCourse;
import com.ckm.service.StudentCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@LogInfo
@Validated
@RestController
@RequestMapping("studentCourse")
public class StudentCourseController {

    @Autowired
    private StudentCourseService studentCourseService;

    /**
     * 学生进行选课
     *
     * @param studentCourse 选课信息
     * @return 成功选课
     */
    @PostMapping("addStudentCourse")
    public ResponseResult addStudentCourse(@RequestBody StudentCourse studentCourse) {
        studentCourseService.chooseStudentCourse(studentCourse);
        return new ResponseResult(200, "选课成功", "");
    }

    /**
     * 查询专业下所有学生分数
     *
     * @param majorName 专业名
     * @param pageNum   页码
     * @param pageSize  页大小
     * @return 所有学生分数分页结果
     */
    @GetMapping("allScore")
    public ResponseResult allStudentCourseScore(@RequestParam(name = "majorName") String majorName, @RequestParam(name = "pageNum", defaultValue = "1") int pageNum, @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        return new ResponseResult(200, majorName + "专业下各学生课程分数",
                studentCourseService.allStudentCourseByMajor(pageNum, pageSize, majorName));
    }

    /**
     * 查询对应学生对应课程分数
     *
     * @param studentCourseInfoDto 查询信息
     * @return 查询结果
     */
    @FindRedis
    @PostMapping("findScore")
    public ResponseResult findStudentCourseScore(@RequestBody StudentCourseInfoDto studentCourseInfoDto, @RequestParam(name = "pageNum", defaultValue = "1") int pageNum, @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        return new ResponseResult(200, "查找课程对应分数",
                studentCourseService.studentScore(studentCourseInfoDto, pageNum, pageSize));
    }

    /**
     * 查询学生课程分数排名
     * @param studentCourseInfoDto 查询参数
     * @param pageNum 页号
     * @param pageSize 页大小
     * @return 查询排名
     */
    @PostMapping("rank")
    public ResponseResult findRankStudentCourseScore(@RequestBody StudentCourseInfoDto studentCourseInfoDto, @RequestParam(name = "pageNum", defaultValue = "1") int pageNum, @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        return new ResponseResult(200, "查找课程对应分数",
                studentCourseService.rankStudentScore(studentCourseInfoDto, pageNum, pageSize));
    }

    @GetMapping("testXss")
    public ResponseResult testXss(@RequestParam("s") String s){
        return new ResponseResult(200,"",s);
    }

    @PostMapping("testXss")
    public ResponseResult testXss(@RequestBody Student student){
        return new ResponseResult(200,"",student);
    }
}
